<?php

define('InAdmin', 1);
$current_page = 'auctions';
include '../includes/common.inc.php';
include $include_path . 'functions_admin.php';
include 'loggedin.inc.php';
include $main_path . 'language/' . $language . '/categories.inc.php';

if (!isset($_REQUEST['id']))
{
	$URL = $_SESSION['RETURN_LIST'];
	unset($_SESSION['RETURN_LIST']);
	header('location: ' . $URL);
	exit;
}

$NOW = time();
$NOWB = gmdate('Ymd');
$catscontrol = new MPTTcategories();

$k = $_GET['id'];

$query = "SELECT duration, category FROM " . $DBPrefix . "auctions WHERE id = " . $k;
$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$AUCTION = mysql_fetch_assoc($res);

// auction ends
$WILLEND = time() + ($AUCTION['duration'] * 24 * 60 * 60);
$suspend = 0;

$query = "UPDATE " . $DBPrefix . "auctions
	  SET starts = '" . $NOW . "',
	  ends = '" . $WILLEND . "',
	  closed = 0,
	  num_bids = 0,
	  relist = relist + relisted + 1,
	  relisted = relisted + 1,
	  current_bid = 0,
	  sold = 'n',
	  suspended = " . $suspend . "
	  WHERE id = " . $k;
$system->check_mysql(mysql_query($query), $query, __LINE__, __FILE__);

// Insert into relisted table
$query = "INSERT INTO " . $DBPrefix . "closedrelisted VALUES (" . $k . ", '" . $NOWB . "', '" . $k . "')";
$system->check_mysql(mysql_query($query), $query, __LINE__, __FILE__);
// delete bids
$query = "DELETE FROM " . $DBPrefix . "bids WHERE auction = " . $k;
$system->check_mysql(mysql_query($query), $query, __LINE__, __FILE__);
// Proxy Bids
$query = "DELETE FROM " . $DBPrefix . "proxybid WHERE itemid = " . $k;
$system->check_mysql(mysql_query($query), $query, __LINE__, __FILE__);
// Winners: only in case of reserve not reached
$query = "DELETE FROM " . $DBPrefix . "winners WHERE auction = " . $k;
$system->check_mysql(mysql_query($query), $query, __LINE__, __FILE__);
// Update COUNTERS table
$query = "UPDATE " . $DBPrefix . "counters SET auctions = auctions + 1";
$system->check_mysql(mysql_query($query), $query, __LINE__, __FILE__);

$query = "SELECT left_id, right_id, level FROM " . $DBPrefix . "categories WHERE cat_id = " . $AUCTION['category'];
$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$parent_node = mysql_fetch_assoc($res);
$crumbs = $catscontrol->get_bread_crumbs($parent_node['left_id'], $parent_node['right_id']);
// update recursive categories
for ($i = 0; $i < count($crumbs); $i++)
{
	$query = "UPDATE " . $DBPrefix . "categories SET sub_counter = sub_counter + 1 WHERE cat_id = " . $crumbs[$i]['cat_id'];
	$system->check_mysql(mysql_query($query), $query, __LINE__, __FILE__);
}

echo "relisted!";

?>
